package com.yulinlin.jdbc.mysql;

import com.yulinlin.core.wrapper.IColumnWrapper;
import com.yulinlin.core.wrapper.IParamsWrapper;
import com.yulinlin.lang.util.StringUtil;

public class MysqlColumnWrapper<E> implements IColumnWrapper<E> {

    private E endWrapper;

    private IParamsWrapper params;

    private StringBuffer sql;


    public MysqlColumnWrapper(E endWrapper, IParamsWrapper params) {
        this.endWrapper = endWrapper;
        this.params = params;
        this.sql = new StringBuffer();

    }



    private void columnHandle(){
        if(this.sql.length() > 0){
            this.sql.append(" , ");
        }
    }

    @Override
    public IColumnWrapper<E> clear(String columnName) {
        this.sql = new StringBuffer();
        return this;
    }

    @Override
    public IColumnWrapper<E> column(String columnName) {

        return columnAsName(columnName,StringUtil.columnToJava(columnName));
    }

    @Override
    public IColumnWrapper<E> asName(String asName) {
        return columnAsName(StringUtil.javaToColumn(asName),asName);
    }



    @Override
    public IColumnWrapper<E> columnAsName(String columnName, String asName) {

        this.columnHandle();

        this.sql.append(columnName+" as "+asName);


        params.alias(asName,columnName);

        return this;
    }

    @Override
    public IColumnWrapper<E> function(String func, String columnName, String asName) {
        this.columnHandle();
        String sqlTemplate =  func;
        String columnFunc =  sqlTemplate.replace("#{columnName}",columnName);
        this.sql.append(columnFunc+" as "+asName);
        return this;
    }

    @Override
    public IColumnWrapper<E> count(String columnName, String asName) {
        return function(MysqlFunctionEnum.count,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> sum(String columnName, String asName) {
        return function(MysqlFunctionEnum.sum,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> avg(String columnName, String asName) {
        return function(MysqlFunctionEnum.avg,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> min(String columnName, String asName) {
        return function(MysqlFunctionEnum.min,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> max(String columnName, String asName) {
        return function(MysqlFunctionEnum.max,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> hour(String columnName, String asName) {
        return function(MysqlFunctionEnum.hour,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> day(String columnName, String asName) {
        return function(MysqlFunctionEnum.day,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> month(String columnName, String asName) {
        return function(MysqlFunctionEnum.month,columnName,asName);
    }

    @Override
    public IColumnWrapper<E> year(String columnName, String asName) {
        return function(MysqlFunctionEnum.year,columnName,asName);
    }

    @Override
    public E end() {
        return endWrapper;
    }

    @Override
    public String getSql() {
        if(sql.length() == 0){
            return " * ";
        }
        return this.sql.toString();
    }

    public void setParams(IParamsWrapper params) {
        this.params = params;
    }

    @Override
    public IParamsWrapper getParams() {
        return params;
    }
}
